package com.sjgtw.cloud.system.controller;

import com.sjgtw.cloud.common.core.constant.UserConstants;
import com.sjgtw.cloud.common.core.domain.R;
import com.sjgtw.cloud.common.core.exception.BaseException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.utils.poi.ExcelUtil;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.log.annotation.Log;
import com.sjgtw.cloud.common.log.enums.BusinessType;
import com.sjgtw.cloud.common.security.annotation.PreAuthorize;
import com.sjgtw.cloud.system.domain.SysConfig;
import com.sjgtw.cloud.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>Title: 系统模块基本参数配置 </p>
 * <p>Description: 前端默认样式、初始化密码等初始化</p>
 * @author Admin
 * @date 2020-7-5
 */
@RestController
@RequestMapping("/config")
public class SysConfigController extends BaseController
{
	@Autowired
	private ISysConfigService configService;

	/**
	 * 获取参数配置列表
	 */
	@PreAuthorize(hasPermi = "system:config:list")
	@GetMapping("/list")
	public TableDataInfo list(SysConfig config) {
		startPage();
		List<SysConfig> list = configService.selectConfigList(config);
		return getDataTable(list);
	}
	
	/**
	 * <p>Title: 导出列表</p>
	 * <p>Description: </p>
	 * @param response
	 * @param config
	 * @throws IOException 
	 */
	@Log(title = "参数管理", businessType = BusinessType.EXPORT)
	@PreAuthorize(hasPermi = "system:config:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfig config) throws IOException
    {
//        List<SysConfig> list = configService.selectConfigList(config);
//        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
//        util.exportExcel(response, list, "参数数据");
        throw new BaseException("配置数据为敏感数据，不提供数据导出功能！");
    }

	/**
	 * 根据参数编号获取详细信息
	 */
	@GetMapping(value = "/{configId}")
	public AjaxResult getInfo(@PathVariable Long configId) {
		return AjaxResult.success(configService.selectConfigById(configId));
	}

	/**
	 * 根据参数键名查询参数值
	 */
	@GetMapping(value = "/configKey/{configKey}")
	public AjaxResult getConfigKey(@PathVariable String configKey) {
		return AjaxResult.success(configService.selectConfigByKey(configKey));
	}

	/**
	 * 根据参数键名查询参数值
	 */
	@GetMapping(value = "/getConfigByKey/{configKey}")
	public R<String> getConfigByKey(@PathVariable String configKey) {
		return R.ok(configService.selectConfigByKey(configKey));
	}

	/**
	 * 新增参数配置
	 */
	@PreAuthorize(hasPermi = "system:config:add")
	@Log(title = "参数管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@Validated @RequestBody SysConfig config) {
		if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
			return AjaxResult.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
		}
		config.setCreateBy(SecurityUtils.getUsername());
		return toAjax(configService.insertConfig(config));
	}

	/**
	 * 修改参数配置
	 */
	@PreAuthorize(hasPermi = "system:config:edit")
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody SysConfig config) {
		if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
			return AjaxResult.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
		}
		config.setUpdateBy(SecurityUtils.getUsername());
		return toAjax(configService.updateConfig(config));
	}

	/**
	 * 删除参数配置
	 */
	@PreAuthorize(hasPermi = "system:config:remove")
	@Log(title = "参数管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{configIds}")
	public AjaxResult remove(@PathVariable Long[] configIds) {
		return toAjax(configService.deleteConfigByIds(configIds));
	}

	/**
	 * 清空缓存
	 */
	@PreAuthorize(hasPermi = "system:config:remove")
	@Log(title = "参数管理", businessType = BusinessType.CLEAN)
	@DeleteMapping("/clearCache")
	public AjaxResult clearCache() {
		configService.clearCache();
		return AjaxResult.success();
	}
	
	/**
     * 刷新参数缓存
     */
	@PreAuthorize(hasPermi = "system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        configService.resetConfigCache();
        return AjaxResult.success();
    }
}
